package com.voipAgent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.packet.VCard;

import android.content.Context;

import com.cvtt.core.SIPCore;
import com.cvtt.core.XMPPCore;
import com.voipAgent.SipAgent.SipEventListener;

public class XmppAgent {
	public static final int U_START = 130;
	public static final int U_STOP = 131;
	public static final int U_RESTART = 132;
	public static final int U_XMPP_LOGIN = 133;
	public static final int U_LOGOUT = 134;
	public static final int U_AWAY = 135;
	public static final int U_RELOGIN = 136;
	public static final int U_LOGIN_SUCCESS = 137;
	public static final int U_LOGIN_FAIL = 138;
	public static final int U_XMPP_KEEP_ALIVE = 139;
	public static final int U_XMPP_EXIT=140;
	public static final int U_ADD_XMPP_CONTACT = 141;
	public static final int U_ACCEPT_XMPP_CONTACT = 142;
	public static final int U_DEL_XMPP_CONTACT = 143;
	public static final int U_LOAD_XMPP_CONTACT_INFO = 144;
	public static final int U_LOAD_XMPP_CONTACTS_INFO = 145;
	public static final int U_UPDATE_XMPP_CONTACT_REMARK = 146;
	public static final int U_LOAD_XMPP_CONTACTS = 147;
	public static final int U_UPDATE_CONTACT_INFO = 148;
	public static final int U_UPDATE_CONTACTS_INFO = 149;
	public static final int U_XMPP_CONTACT_ADDED = 150;
	public static final int U_SET_INFO=151;

	public static final int U_SEND_MSG = 152;// 发送消息
	public static final int U_RECV_MSG = 153;// 收到消息
	
	public static final int U_UPDATE_XMPP_STATUS = 154;
	
	private static XmppAgent xmppAgent;
	private static Context mContext;
	private XMPPCore xmppCore;
	


	public static XmppAgent getInstance(Context context,XmppEventListener eventListener){
		if(xmppAgent==null){
			xmppAgent=new XmppAgent(context,eventListener);
		}
		mContext=context;
		return xmppAgent;
	}
	
	public XmppAgent(Context context, XmppEventListener eventListener) {
		// TODO Auto-generated constructor stub
		xmppCore = XMPPCore.getInstance(context,eventListener);
	}
	public void start(){
		xmppCore.postCoreEvent(XmppAgent.U_START, null);
	}
	public void reStart(){
		xmppCore.postCoreEvent(XmppAgent.U_RESTART, null);
	}
	
	
	public void login(String userName,String password){
		HashMap<String, String> data = new HashMap<String, String>();
		data.put(XMPPCore.XMPP_USER_NAME, userName);
		data.put(XMPPCore.XMPP_USER_PASSWORD, password);
		xmppCore.postCoreEvent(XmppAgent.U_XMPP_LOGIN, data);
	}
	public void reLogin(){
		xmppCore.postCoreEvent(XmppAgent.U_RELOGIN, null);
	}
	
	public void logout(){
		xmppCore.postCoreEvent(XmppAgent.U_LOGOUT, null);
	}
	
	public void exit(){
		xmppCore.postCoreEvent(XmppAgent.U_XMPP_EXIT, null);
	}
	public void setInfo(){
		xmppCore.postCoreEvent(XmppAgent.U_SET_INFO, null);
	}
	
	public void sendMessage(TZMessage data){
		
		xmppCore.postCoreEvent(XmppAgent.U_SEND_MSG, data);
	}
	
	public boolean isXMPPOnline(){
		return xmppCore.isOnline();
	}
	public interface XmppEventListener{
		public void onXMPPLoginSuccess();
		public void onXMPPLoginError();
		public void onXMPPRecvMessage(TZMessage message);
		public void onXMPPSendMessageSuccess(String msgID);
		public void onXMPPSendMessageError(String msgID);
		public void onXMPPInfoLoad(VCard info);
		public void onXMPPAddContact(String jid, String remark);
		public void onXMPPContactAdded(RosterEntry rosterEntry);
		public void onXMPPContactDeleted(String uNumber);
		public void onXMPPContactsLoaded(Collection<RosterEntry> xmppRosters);
		public void onXMPPContactInfoLoaded(VCard info);
		public void onXMPPContactsInfoLoaded(ArrayList<VCard> contactsInfo);
		
		public void onXMPPKick();
		public void onXMPPIsOnline(boolean isOnline);
		public void onXMPPUpdateStatus();
		
	}

}
